package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.constants.ErrorConstants;
import com.woniu.entity.SysConfig;
import com.woniu.exception.MyErrorException;
import com.woniu.form.SysConfigForm;
import com.woniu.mapper.SysConfigMapper;
import com.woniu.service.SysConfigService;
import com.woniu.utils.ConverterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author: woniu
 * @date: 2023/12/28 10:13
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Autowired
    private SysConfigMapper sysConfigMapper;

    /**
     * 更新系统配置
     * @param form
     * @return
     */
    @Override
    public boolean editConfig(SysConfigForm form) {

        if (!form.getSign().equals("cb28a6a9-eae6-4ffd-90ae-8ae1daf2995d")){
            throw new MyErrorException(ErrorConstants.SYS_SIGN_ERROR);
        }

        if (form.getId() == null){
            throw new MyErrorException(ErrorConstants.SYS_CONFIG_NULL);
        }

        return sysConfigMapper.update(null,new LambdaUpdateWrapper<SysConfig>()
                .eq(SysConfig::getId,form.getId())
                .set(form.getNewUserNumber() != null,SysConfig::getNewUserNumber,form.getNewUserNumber())
                .set(form.getRedEinmalNumber() != null,SysConfig::getRedEinmalNumber,form.getRedEinmalNumber())
                .set(form.getRedEverydayMaxNum() != null,SysConfig::getRedEverydayMaxNum,form.getRedEverydayMaxNum())
                .set(form.getRedOneMax() != null,SysConfig::getRedOneMax,form.getRedOneMax())
                .set(form.getRedEinmalTotalMoney() != null,SysConfig::getRedEinmalTotalMoney,form.getRedEinmalTotalMoney())
                .set(form.getRegulationContent() != null,SysConfig::getRegulationContent,form.getRegulationContent())
                .set(form.getRedOneMinNum() != null,SysConfig::getRedOneMinNum,form.getRedOneMinNum())
                .set(form.getZeroPointRedNumber() != null,SysConfig::getZeroPointRedNumber,form.getZeroPointRedNumber())
                .set(form.getRedOneMin() != null,SysConfig::getRedOneMin,form.getRedOneMin())
                .set(form.getRedEverydayMaxMoney() != null,SysConfig::getRedEverydayMaxMoney,form.getRedEverydayMaxMoney())
                .set(form.getRedOneMaxNum() != null,SysConfig::getRedOneMaxNum,form.getRedOneMaxNum())
                .set(form.getRedOneRandomMin() != null,SysConfig::getRedOneRandomMin,form.getRedOneRandomMin())
                .set(form.getRedOneRandomMax() != null,SysConfig::getRedOneRandomMax,form.getRedOneRandomMax())
                .set(form.getRedOneRandomNum() != null,SysConfig::getRedOneRandomNum,form.getRedOneRandomNum())
                .set(form.getRedOneRandomTotalMoney() != null,SysConfig::getRedOneRandomTotalMoney,form.getRedOneRandomTotalMoney())) > 0;
    }

    /**
     * 新增系统配置
     * @param form
     * @return
     */
    @Override
    public boolean saveConfig(SysConfigForm form) {
        if (!form.getSign().equals("cb28a6a9-eae6-4ffd-90ae-8ae1daf2995d")){
            throw new MyErrorException(ErrorConstants.SYS_SIGN_ERROR);
        }

        SysConfig sysConfig = ConverterUtil.convert(form, SysConfig.class);

        return sysConfigMapper.insert(sysConfig) > 0;
    }

    /**
     * 获取最新的配置
     * @param sign 密钥
     * @return
     */
    @Override
    public SysConfig newestConfig(String sign) {
        if (!sign.equals("cb28a6a9-eae6-4ffd-90ae-8ae1daf2995d")){
            throw new MyErrorException(ErrorConstants.SYS_SIGN_ERROR);
        }
        return sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .orderByDesc(SysConfig::getUpdateTime)
                .last(" limit 1"));
    }
}
